#!/bin/sh
# Job yaml can send below ENVIRONMENT VARIABLES to me
# - tbranch
# - tarch
# - plan
# - pr_num
# - package
# - os_project
# - base_rpm_url
# - check_rpm_url
# - base_rpms
# - check_rpms
# END ENVIRONMENT VARIABLES

## Oecp is a tool to check the driver kabi in openeuler released kabi whitelist or not.
## Check kernel kabi in whitelist or not.
## Check pkgs compatibility change.
## Check the influence of pkg compatibility change.

. $LKP_SRC/lib/upload.sh

report=/tmp/oecp
CLI_SCRIPT=${HOME}/oecp
PLAN_PATH=${CLI_SCRIPT}/oecp/conf/plan
base_pkg_dir=${HOME}/pkg_download
LOG=/var/log/oecp
CCB=/ccb

function deploy_oecp_tool() {
  echo "========== Start install oecp tool =========="
  local oecp_url="git://${GIT_SERVER}/oecp"
  git clone -q $oecp_url $CLI_SCRIPT
  pip3 install -r ${CLI_SCRIPT}/requirement -i https://mirrors.huaweicloud.com/repository/pypi/simple
  echo "========== Install oecp done =========="
}

function clone_ccb() {
  rm -rf ${CCB}
  git clone https://gitee.com/yang-yulong-007/ccb.git ${CCB}
  cp -r ${CCB}/sbin/cli ${LKP_SRC}/sbin/
  mkdir ${LKP_SRC}/sbin/lib
  cp -r ${CCB}/lib/opt_parse.rb ${LKP_SRC}/sbin/lib/
}

function write_cli_config() {
  export OAUTH_TOKEN_URL='https://omapi.osinfra.cn/oneid/oidc/token'
  export OAUTH_REDIRECT_URL='https://123.249.10.3:30108/oauth/'
  export PUBLIC_KEY_URL='https://omapi.osinfra.cn/oneid/public/key?community=openeuler'

  default="${HOME}/.config/cli/defaults/"
  mkdir -p $default
  cat >${default}/config.yaml <<EOF
GATEWAY_PORT: 443
GATEWAY_IP: eulermaker.compass-ci.openeuler.openatom.cn
SRV_URL: https://eulermaker.compass-ci.openeuler.openatom.cn
SRV_HTTP_REPOSITORIES_PROTOCOL: https://
SRV_HTTP_REPOSITORIES_HOST: eulermaker.compass-ci.openeuler.openatom.cn
SRV_HTTP_REPOSITORIES_PORT: 443
EOF
}

function ccb_download_binarys() {
  echo "============Start download binarys============"
  clone_ccb
  write_cli_config
  project=${os_project}
  if [[ "x${package}" == "xkernel" ]]; then
    package="kernel:kernel"
  fi
  download_result=$(${CCB}/sbin/cli/ccb download os_project=$project packages=$package architecture=$tarch -d -b all)
  if [[ $? -ne 0 || -n $(echo $download_result | grep "Not found") ]]; then
    echo "Failed to download the RPM package generated by $package project $project arch $tarch"
    return 1
  fi
  binary_folder=$project-$tarch-$package
  echo "Download the RPM package generated by $package project $project arch $tarch."
  if [[ -d $binary_folder && "$(ls -A $binary_folder | grep '\.rpm$')" ]]; then
    for rpm in $(ls -A $binary_folder | grep '\.rpm$'); do
      right_rpm=$(echo ${rpm%-*-*})
      echo $package":"$right_rpm
    done
    cp $binary_folder/*.rpm ${base_pkg_dir}/check_dir
    ls $binary_folder/
    sudo rm -rf ./$binary_folder
  fi
  echo "<========================================================>"
  ls -l ${base_pkg_dir}/check_dir
  echo "<========================================================>"
  echo "The RPM package generated by the $project is successfully downloaded."
}

function wget_check_rpms() {
  mkdir -p ${base_pkg_dir}/{base_dir,check_dir}
  echo "========== start download all base rpms =========="
  if [ -n "${base_rpm_url}" ]; then
    if [ -z "${base_rpms}" ]; then
      wget -r -nd -np -L -nH --cut-dirs=1 -nv -P ${base_pkg_dir}/base_dir -R *debuginfo*.rpm,*debugsource*.rpm,*.src.rpm -A *.rpm ${base_rpm_url} && echo "download base rpms success" || echo "wget base rpms error and url is ${base_rpm_url}"
    else
      for base_rpm in ${base_rpms}; do
        wget -P ${base_pkg_dir}/base_dir -nv "${base_rpm_url}/${base_rpm}" || echo "${base_rpm_url}/${base_rpm} download rpm error"
      done
    fi
  else
    echo "base_rpms url is null:${base_rpm_url}"
  fi
  
  echo "========== start download all check rpms =========="
  if [ -n "${os_project}" ]; then
    ccb_download_binarys
  else
    if [ -n "${check_rpm_url}" ]; then
      if [ -z "${check_rpms}" ]; then
        wget -r -nd -np -L -nH --cut-dirs=1 -nv -P ${base_pkg_dir}/check_dir -R *debuginfo*.rpm,*debugsource*.rpm,*.src.rpm -A *.rpm ${check_rpm_url} && echo "download check rpms success" || echo "wget check rpms error and url is ${check_rpm_url}"
      else
        for check_rpm in ${check_rpms}; do
          wget -P ${base_pkg_dir}/check_dir -nv "${check_rpm_url}/${check_rpm}" || echo "${check_rpm_url}/${check_rpm} download error"
        done
      fi
    else
      echo "check rpms url is null."
    fi
  fi
}

function run_oecp_check() {
  echo "========== Start run oecp check =========="
  if [[ $pr_num -eq 1 && "$(ls -A ${base_pkg_dir}/check_dir | grep '.rpm')" ]]; then
    echo "Pr 1 not exist last version rpms, oecp run with check rpms..."
    python3 ${CLI_SCRIPT}/cli.py -p ${PLAN_PATH}/${plan}.json ${base_pkg_dir}/check_dir ${base_pkg_dir}/check_dir --branch-arch ${tbranch}-${tarch}
  elif [[ "$(ls -A ${base_pkg_dir}/base_dir | grep '.rpm')" && "$(ls -A ${base_pkg_dir}/check_dir | grep '.rpm')" ]]; then
    echo "Oecp run compare base rpms with check rpms..."
    python3 ${CLI_SCRIPT}/cli.py -p ${PLAN_PATH}/${plan}.json ${base_pkg_dir}/base_dir ${base_pkg_dir}/check_dir --branch-arch ${tbranch}-${tarch}
  else
    echo "Not download check rpms, can't run oecp to compare check rpms."
  fi

  find $report -name "osv.json" | xargs mv -t $report
  echo "========== Finish oecp check =========="
}

echo "========== Start oecp check ${plan} task =========="
set -x
deploy_oecp_tool
wget_check_rpms
run_oecp_check

cmp_result="FAILED"
if [ -f ${report}/osv.json ]; then
  echo -e "osv.json report:\n $(cat ${report}/osv.json)"
  diff_num=$(cat ${report}/osv.json | grep diff_num | awk -F ":" '{print $2}')
  if [ $diff_num -eq 0 ]; then
    cmp_result="SUCCESS"
  fi
  upload_files -t oecp-results ${report}/osv.json
else
  echo "Not found the output report: osv.json"
fi
if [ -d ${report}/details_analyse ]; then
  upload_files -t oecp-results ${report}/details_analyse
fi

echo "Result: ${cmp_result}"
set +x
echo "========== Finish oecp check ${plan} task =========="
